﻿using DatabaseUserTest.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseUserTest.Controller
{
    public class OrderCTRL
    {
        private DBOrder orderDb = new DBOrder();

        public Order FindOrderByPhone(string phoneNo)
        {
            int result;
            if (phoneNo.Length == 8 && int.TryParse(phoneNo, out result))
            {
                if (orderDb.FindOrder(result) != null)
                {
                    return orderDb.FindOrder(result);
                }
            }
            return null;
        }

        public Order FindOrderByMail(string mail)
        {
            RegexUtilities mailcheck = new RegexUtilities();
            if (mailcheck.IsValidEmail(mail))
            {
                if (orderDb.FindOrder(mail) != null)
                {
                    return orderDb.FindOrder(mail);
                }
            }
            return null;  
        }

        public bool AddOrder(int totalPrice, string phoneNo, string route)
        {
            DBUser userDb = new DBUser();
            DBPlane planeDb = new DBPlane();
            int result;
            if (phoneNo.Length == 8 && int.TryParse(phoneNo, out result) && userDb.FindUser(result) != null && totalPrice > 0 && route != null)
            {
                User user = userDb.FindUser(result);
                Order order = new Order();
                order.TotalPrice = totalPrice;
                order.UserId = user.UserId;
                order.User = user;
                order.Route = route;
                orderDb.AddOrder(order);
                if (orderDb.FindOrder(user.PhoneNo) != null)
                {
                    return true;
                }

            }
            return false;
        }
    }
}
